import { defineStore } from "pinia";
import { getAllDormitoryApi, isDormitoryUsedApi, getAllNumbersApi } from '@/service/dormitory'
import { getBuildingsDataApi } from '@/service/buildings'
export const useDormitoryStore = defineStore('dormitoryStore', {
    state() {
        return {
            tableData: [],
            isShowDormitoryPanel: false, 
            isDialogVisible: false,
            panelTitle: '',
            buildings: [],
            isUsed: false,  // 宿舍号是否被使用
            allNumbers: [],
            build: '',
            roomName: ''
        }
    },
    actions: {
        // 获取所有的宿舍管理数据
        async getAllDormitory() {
            const res = await getAllDormitoryApi();
            const { data } = res.data;
            this.tableData = data;
        },
        // 获取所有的宿舍楼信息
        async getAllBuilding() {
            const res = await getBuildingsDataApi(1);
            const { data } = res.data;
            this.buildings = data;
        },
        // 搜索功能
        async searchDormitory(building, room_name) {
            this.tableData = [];
            const res = await isDormitoryUsedApi(building, room_name);
            const { code, data } = res.data;
            if (code) {
                this.tableData.push(data);
            }
        },
        // 获取宿舍的所有成员
        async getAllNumbers(building, room_name) {
            const res = await getAllNumbersApi(building,room_name);
            const { data } = res.data;
            this.allNumbers = data;
        }
    }
})  